const http = require('../../utils/http')
const api = require('../../utils/api')
// pages/rechange.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantId: '',
    numberFocus: false,
    moneyFocus: false,
    numberValue: '',
    moneyValue: '',
    remaining: '',
    btnText: '账户查询',
    showMoney: false,
    memberId: '',
  },
  numInput: function (e) {
    this.setData({
      numberValue: e.detail.value
    })
    console.log(this.data.numberValue + "--------------")
  },
  moneyInput: function (e) {
    this.setData({
      moneyValue: e.detail.value
    })
    console.log(this.data.moneyValue + "////////")
  },
  closeview: function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  confirm: function(e){
    if(this.data.showMoney){
      this.doRecharge();
    }else{
      this.checkUser();
    }
  },
  checkUser: function(e){
    let that = this;
    if(this.data.numberValue == ''){
      wx.showToast({
        title: '请先输入用户手机号',
        icon: "none",
        duration: 2000
      });
    }else{
      http.request({
        showLoading: true,
        url: api.moneyCheck,
        data: {
          merchantId: this.data.merchantId,
          mobile: this.data.numberValue,
        },
      }).then(res => {
        if (res.data.code == 200) {
          that.setData({
            showMoney: true,
            remaining: res.data.result.money,
            memberId: res.data.result.memberId,
            btnText: '确认充值',
            moneyFocus: true
          })
        }else if(res.data.code == 1001){
          wx.showToast({
            title: '当前用户未注册',
            icon: "none",
            duration: 2000
          });
          that.setData({
            numberValue: "",
          })
        }else{
          wx.showToast({
            title: '请求失败,请稍后再试',
            icon: "none",
            duration: 2000
          });
        }
      }).catch(err => {
        console.log(err)
      });
    }
  },
  doRecharge: function(e){
    if(this.data.moneyValue == ''){
      wx.showToast({
        title: '请先输入充值金额',
        icon: "none",
        duration: 2000
      });
    }else{
      http.request({
        showLoading: true,
        url: api.moneyRecharge,
        method: 'POST',
        data: {
          memberId: this.data.memberId,
          amount: this.data.moneyValue,
        },
      }).then(res => {
        if (res.data.code == 200) {
          setTimeout(function () {
            wx.showToast({
              title: '充值成功',
              icon: "none",
              duration: 2000
            });
          }, 1000);
          wx.navigateBack({
            delta: 1,
          })
        }else{
          wx.showToast({
            title: '请求失败,请稍后再试',
            icon: "none",
            duration: 2000
          });
        }
      }).catch(err => {
        console.log(err)
      });
    }
  },
  reset: function(e){
    this.setData({
      showMoney: false,
      btnText: '账户查询',
      numberFocus: true
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    console.log(options);
    that.setData({
      merchantId: options.merchantId,
      numberFocus: true,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})